/**
 * 设备功能工具
 */
var DeviceUtil = {};
/**
 * 获得电池电量
 */
DeviceUtil.getBatteryLevel = function () {
    var level = '100';
    try {
        if (cc.sys.os == cc.sys.OS_IOS) {
            level = jsb.reflection.callStaticMethod('CommonUtil', 'getBatteryLevel');
        } else if (cc.sys.os == cc.sys.OS_ANDROID) {
            level = jsb.reflection.callStaticMethod(packageUri + '/utils/CommonUtil', 'getBatteryLevel', '()Ljava/lang/String;');
        }
    }catch (e) {
    }
    return Math.floor(level);
};
/**
 * 注册android返回键
 */
DeviceUtil.regBakBtn = function () {
    if (cc.sys.os == cc.sys.OS_ANDROID) {
        cc.eventManager.addListener({
            event: cc.EventListener.KEYBOARD,
            onKeyReleased: function (keyCode) {
                if (keyCode == cc.KEY.back) {
                    if (!window.exitBox) {
                        window.exitBox = showMessageBox('确定退出游戏吗?', function () {
                            cc.director.end();
                        }, function () {
                            window.exitBox = null;
                        });
                    }
                }
            }
        }, 1);
    }
};
DeviceUtil.getSearchPaths = function () {
    return cc.sys.isNative ? jsb.fileUtils.getSearchPaths() : [];
};
DeviceUtil.addSearchPath = function (path) {
    if (!cc.sys.isNative)
        return;
    var searchPaths = jsb.fileUtils.getSearchPaths();
    if (searchPaths.indexOf(path) < 0)
        searchPaths.splice(0, 0, path);
    jsb.fileUtils.setSearchPaths(searchPaths);
};
DeviceUtil.fetchUDID = function () {
    if (!cc.sys.isNative) {
        return '0123456789abcdef';
    }
    if (cc.sys.os == cc.sys.OS_IOS) {
        return jsb.reflection.callStaticMethod(
            'CommonUtil',
            'fetchUDID'
        );
    }
    else {
        return jsb.reflection.callStaticMethod(
            packageUri + '/utils/CommonUtil',
            'fetchUDID',
            '()Ljava/lang/String;'
        );
    }
};

/**
 * 获取游戏盾ip（SDK v5.3.2）
 * @param {String} app_key
 * @param {String} group
 * @return {String} ip
 * */
DeviceUtil.getNextIPByGroup = function (app_key, group) {
    if (!cc.sys.isNative) {
        return null;
    }
    var udid = DeviceUtil.fetchUDID();
    if (cc.sys.os === cc.sys.OS_IOS) {
        try {
            return jsb.reflection.callStaticMethod(
                'CommonUtil',
                'ni2:n:token:port:l:forceInit:',
                app_key,
                group,
                udid,
                app.port,
                0,
                false
            );
        } catch (e) {
        }
    }
    else {
        // return jsb.reflection.callStaticMethod(
        //     'org/cocos2dx/javascript/AppActivity',
        //     'ni2',
        //     '(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)Ljava/lang/String;',
        //     app_key,
        //     group,
        //     udid,
        //     '' + app.port,
        //     0
        // );
        return DeviceUtil.getNextIp2(app_key, group);
    }
};

/**
 * 获取游戏盾ip（v5.3.2之前使用）
 * @param {String} app_key
 * @param {String} group
 * @return {String} ip
 * */
DeviceUtil.getNextIp2 = function (app_key, group) {
    if (!cc.sys.isNative) {
        return null;
    }
    if (cc.sys.os == cc.sys.OS_IOS) {
        try {
            return jsb.reflection.callStaticMethod(
                'CommonUtil',
                'ni2:n:',
                app_key,
                group
            );
        } catch (e) {
        }
    }
    else {
        return jsb.reflection.callStaticMethod(
            'org/cocos2dx/javascript/AppActivity',
            'ni2',
            '(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;',
            app_key,
            group
        );
    }
};
/**
 * 複製文本到剪切板
 * @param {String} text
 */
DeviceUtil.savePasteBoard = function (text) {
    if (cc.sys.os == cc.sys.OS_IOS) {
        jsb.reflection.callStaticMethod(
            'CommonUtil',
            'savePasteBoard:',
            text
        );
    } else if (cc.sys.os == cc.sys.OS_ANDROID) {
        jsb.reflection.callStaticMethod(
            packageUri + '/utils/ClipboardUtil',
            'clipboardCopyText',
            '(Ljava/lang/String;)V',
            text
        );
    }
};

/**
 * 複製文本到剪切板
 * @return {String}
 */
DeviceUtil.getPasteBoard = function () {
    if (cc.sys.os === cc.sys.OS_IOS) {
        return jsb.reflection.callStaticMethod('CommonUtil', 'getPasteBoard');
    } else if (cc.sys.os === cc.sys.OS_ANDROID) {
        return jsb.reflection.callStaticMethod(packageUri + '/utils/ClipboardUtil', 'getPasteBoard', '()Ljava/lang/String;');
    }
    return '';
};

/**
 * 打开指定的app
 * @param {String} app
 */
DeviceUtil.openApp = function (app) {
    if (typeof app === 'undefined') {
        app = cc.sys.os === cc.sys.OS_IOS ? 'weixin://' : 'com.tencent.mm';
    }
    if (cc.sys.os === cc.sys.OS_IOS) {
        jsb.reflection.callStaticMethod('CommonUtil', 'openApp:', app);
    } else if (cc.sys.os === cc.sys.OS_ANDROID) {
        jsb.reflection.callStaticMethod(packageUri + '/utils/CommonUtil', 'openApp', '(Ljava/lang/String;)Z', app);
    }
};

/**
 * 是否是debug状态
 * @returns {Boolean}
 */
DeviceUtil.getDebug = function () {
    if (cc.sys.os == cc.sys.OS_IOS) {
        return jsb.reflection.callStaticMethod('CommonUtil', 'getDEBUG');
    } else if (cc.sys.os == cc.sys.OS_ANDROID) {
        return false;
    } else if (!cc.sys.isNative) {
        return true;
    }
};
/**
 * 震动
 */
DeviceUtil.vibrate = function (time) {
    time = time || 1;
    if (cc.sys.os == cc.sys.OS_IOS) {
        cc.Device.vibrate(time);
    } else if (cc.sys.os == cc.sys.OS_ANDROID) {
        cc.Device.vibrate(time);
    }
};